from pyecharts import options as opts
from pyecharts.charts import Bar,Pie,Line

from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from analyse import house_analyse

def num_proportion():
    data_names = ['思明','湖里','集美','海沧','翔安','同安','杏林']
    data_lengths = []
    for data_name in data_names:
        filename = data_name+".csv"
        with open(filename,encoding="utf-8") as f:
            data_lengths.append(len(f.readlines()))

    c = (
        Pie()
            .add("", [list(z) for z in zip(data_names,data_lengths)])
            .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
            .set_global_opts(title_opts=opts.TitleOpts(title="每个区的租房数量占比"))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
            # .render("每个区的租房数量占比.html")
    )
    make_snapshot(snapshot, c.render(), "每个区的租房数量占比.png")

def price_avg():
    data_names = ['思明','湖里','集美','海沧','翔安','同安','杏林']
    data_res = []
    for data_name in data_names:
        filename = data_name+".csv"
        data_res.append(house_analyse(filename))
    data_mean = [i['mean'] for i in data_res]
    c = (
        Bar()
            .add_xaxis(data_names)
            .add_yaxis("最平均值", data_mean, stack="stack3")
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(title_opts=opts.TitleOpts(title="各区的房租金额平均值"))
            # .render("各区的房租金额平均值.html")
    )
    make_snapshot(snapshot, c.render(), "各区的房租金额平均值.png")

def price_mid():
    data_names = ['思明','湖里','集美','海沧','翔安','同安','杏林']
    data_res = []
    for data_name in data_names:
        filename = data_name+".csv"
        data_res.append(house_analyse(filename))
    data_mid = [i['mid'] for i in data_res]
    c = (
        Line()
            .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
        )
            .add_xaxis(xaxis_data=data_names)
            .add_yaxis(
            series_name="",
            y_axis=data_mid,
            symbol="emptyCircle",
            is_symbol_show=True,
            label_opts=opts.LabelOpts(is_show=False),
        )
            # .render("各区的房租金额中位数.html")
    )
    make_snapshot(snapshot, c.render(), "各区的房租金额中位数.png")

def price_max():
    data_names = ['思明','湖里','集美','海沧','翔安','同安','杏林']
    data_res = []
    for data_name in data_names:
        filename = data_name+".csv"
        data_res.append(house_analyse(filename))
    data_max = [i['max'] for i in data_res]
    c = (
        Bar()
            .add_xaxis(data_names)
            .add_yaxis("最大值", data_max)
            .reversal_axis()
            .set_series_opts(label_opts=opts.LabelOpts(position="right"))
            .set_global_opts(title_opts=opts.TitleOpts(title="各区的房租金额最大值"))
            # .render("各区的房租金额最大值.html")
    )
    make_snapshot(snapshot, c.render(), "各区的房租金额最大值.png")

def price_min():
    data_names = ['思明','湖里','集美','海沧','翔安','同安','杏林']
    data_res = []
    for data_name in data_names:
        filename = data_name+".csv"
        data_res.append(house_analyse(filename))
    data_max = [i['min'] for i in data_res]
    c = (
        Bar()
            .add_xaxis(data_names)
            .add_yaxis("最大值", data_max)
            .reversal_axis()
            .set_series_opts(label_opts=opts.LabelOpts(position="right"))
            .set_global_opts(title_opts=opts.TitleOpts(title="各区的房租金额最小值"))
            # .render("各区的房租金额最小值.html")
    )
    make_snapshot(snapshot, c.render(), "各区的房租金额最小值.png")

if __name__ == '__main__':
    # price_avg()
    price_min()
    # price_max()
    # num_proportion()
    # price_mid()
